﻿using System;
using System.Linq;
using Biz.Buffer;
using Biz.EF.Gateway.Monitor;

using Biz.EF.Platform.SG;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class ProcessorDownline
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorDownline));
        public static void GenDownlineCmdToGateway(GatewayMonitorContext gCtx, PlatformXpfContext pCtx)
        {
            try
            {
                var cmds = pCtx.阀门控制.Where(t => t.执行结果标志ID == 601).Take(500).ToList();

                foreach (var downcmd in cmds)
                {
                    downcmd.执行结果标志ID = 602;
                    var cn = "";
                    switch (downcmd.阀门执行事项ID)
                    {
                        case 501:
                            cn = "3051";
                            break;
                        case 502:
                            cn = "3071";
                            break;
                    }
                    var port = CommonInformation.GetLetportById(downcmd.企业排口ID);
                    if (port == null) continue;

                    var cp = "";
                    if (cn.Equals("3051"))
                    {
                        cp = "Status=" + downcmd.阀门状态;
                    }
                    var resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=" + cn + ";PW=123456" + ";MN=" + port.MN号
                                        + ";Flag=0;CP=&&" + cp + "&&";
                    var result = Crc.WrapByCrc(resultStr,"total");
                    var now = DateTime.Now;
                    send_ctr_cmd cmd = new send_ctr_cmd
                    {
                        cn = cn,
                        mn = port.MN号,
                        qn = downcmd.QN,
                        is_sent = false,
                        send_string = result,
                        sent_time = now,
                        send_time = now
                    };
                    gCtx.send_ctr_cmd.Add(cmd);
                    gCtx.SaveChanges();
                    if (cn.Equals("3051"))
                    {
                        cp = "Per=" + (int.Parse(downcmd.阀门状态) * 100).ToString("D3");
                        resultStr = "QN=" + downcmd.QN + ";ST=32;" + "CN=" + cn + ";PW=123456" + ";MN=" + port.MN号
                                        + ";Flag=0;CP=&&" + cp + "&&";
                        result = Crc.WrapByCrc(resultStr,"total");
                        cmd = new send_ctr_cmd
                        {
                            cn = cn,
                            mn = port.MN号,
                            qn = downcmd.QN,
                            is_sent = false,
                            send_string = result,
                            sent_time = now,
                            send_time = now
                        };
                        gCtx.send_ctr_cmd.Add(cmd);
                        gCtx.SaveChanges();
                    }
                }
                pCtx.SaveChanges();
              
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }

    }
}
